<?php


namespace app\api\controller\hd;

use app\common\controller\Api;
use think\Db;
use \app\admin\model\hd\information\Info as InfoModel;
use \app\admin\model\hd\information\Type as TypeModel;
use \app\admin\model\hd\information\Zan as ZanModel;
use \app\admin\model\hd\information\Collect as CollectModel;
use \app\admin\model\hd\information\comment\Info as commentInfoModel;
use \app\admin\model\hd\information\comment\Zan as commentZanModel;


class Information extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 活动页新闻
     */
    public function index()
    {
        $list = InfoModel::with("type")->where("status", "normal")->order("create_time", "desc")->paginate(4);
        $this->success('获取成功', $list);
    }
    /**
     * 新闻列表
     */
    public function list($type_id = 0, $name = null)
    {
        $where = [];
        if ($type_id != 0) {
            $where["type_id"] = $type_id;
        }
        if ($name != null) {
            $list = InfoModel::with("type")
                ->where($where)
                ->where(InfoModel::getTable() . ".name", "like", "%$name%")
                ->where("status", "normal")->order("create_time", "desc")
                ->paginate();
        } else {
            $list = InfoModel::with("type")
                ->where($where)
                ->where("status", "normal")
                ->order("create_time", "desc")
                ->paginate();
        }
        $this->success('获取成功', $list);
    }

    /**
     * 新闻详情
     */
    public function details($id)
    {
        $details = InfoModel::with("type")->where(InfoModel::getTable() . ".id", $id)->find();
        $details['collection_status'] = false;
        $details['zan_status'] = false;
        $details['zan_num'] = ZanModel::where("in_id", $id)->count();
        // $this->success('获取成功', $this->auth->id);
        if ($this->auth->id) {
            $Collect = CollectModel::where("in_id", $id)->where("user_id", $this->auth->id)->find();
            if ($Collect) {
                $details['collection_status'] = true;
            }
            $Zan = ZanModel::where("in_id", $id)->where("user_id", $this->auth->id)->find();
            if ($Zan) {
                $details['zan_status'] = true;
            }
        }
        $this->success('获取成功', $details);
    }

    /**
     * 新闻浏览
     */
    public function browse($id)
    {
        InfoModel::where("id", $id)->setInc("see_num");
    }

    /**
     * 新闻评论列表
     */
    public function commentList($id)
    {

        $list = commentInfoModel::where("in_id", $id)
            ->paginate()
            ->each(function ($comment, $key) {
                if ($this->auth->_logined == true) {
                    $comment->zan_status = false;
                    $Zan = commentZanModel::where("co_id", $comment->id)->where("user_id", $this->auth->id)->find();
                    if ($Zan) {
                        $comment->zan_status = true;
                    }
                }
            });
        $this->success('获取成功', $list);
    }

    /**
     * 新闻评论提交
     */
    public function commentSubmit()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['user_id'] = $this->auth->id;
            $params['create_time'] = time();
            $insert = commentInfoModel::insert($params);
            if ($insert) {
                $this->success('评论成功');
            }
            $this->error('评论失败');
        }
        $this->error('非法请求');
    }

    /**
     * 新闻评论删除
     */
    public function commentDel($id)
    {
        $delete = commentInfoModel::where("id", $id)->delete();
        if ($delete) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }



    /**
     * 新闻评论点赞
     */
    public function comment_zan($id)
    {
        $collect =  commentZanModel::where("co_id", $id)->where("user_id", $this->auth->id)->find();
        if ($collect) {
            $delete = commentZanModel::where("co_id", $id)->where("user_id", $this->auth->id)->delete();
            if ($delete) {
                $this->success('删除成功');
            }
        } else {
            $insert = commentZanModel::insert(["co_id" => $id, "user_id" => $this->auth->id, "create_time" => time()]);
            if ($insert) {
                $this->success('点赞成功');
            }
        }
        $this->error('操作失败');
    }

    /**
     * 新闻点赞
     */
    public function zan($id)
    {
        $Zan =  ZanModel::where("in_id", $id)->where("user_id", $this->auth->id)->find();
        if ($Zan) {
            $delete = ZanModel::where("in_id", $id)->where("user_id", $this->auth->id)->delete();
            if ($delete) {
                $this->success('删除成功');
            }
        } else {
            $insert = ZanModel::insert(["in_id" => $id, "user_id" => $this->auth->id, "create_time" => time()]);

            if ($insert) {
                $this->success('点赞成功');
            }
        }
        $this->error('操作失败');
    }
    /**
     * 新闻收藏
     */
    public function collect($id)
    {
        $collect =  CollectModel::where("in_id", $id)->where("user_id", $this->auth->id)->find();
        if ($collect) {
            $delete = CollectModel::where("in_id", $id)->where("user_id", $this->auth->id)->delete();
            if ($delete) {
                InfoModel::where("id", $id)->setDec("collect_num");
                $this->success('删除成功');
            }
        } else {
            $insert = CollectModel::insert(["in_id" => $id, "user_id" => $this->auth->id, "create_time" => time()]);
            if ($insert) {
                InfoModel::where("id", $id)->setInc("collect_num");
                $this->success('收藏成功');
            }
        }
        $this->error('操作失败');
    }
    /**
     * 新闻类型
     */
    public function type()
    {
        $list = TypeModel::select();
        $this->success('获取成功', $list);
    }

    /**
     * 新闻分享
     */
    public function fanxiang($id)
    {
        InfoModel::where("id", $id)->setInc("shares_num");
    }

    /**
     * 获取新闻收藏列表
     */
    public function collectList($type_id = 0, $name = null)
    {
        // 获取当前用户收藏的数据
        $collect = CollectModel::where("user_id", $this->auth->id)->select();

        // 提取出用户收藏的 in_id 列表
        $asd = [];
        foreach ($collect as $key => $value) {
            $asd[] = $value->in_id;
        }
        $where = [];
        if ($type_id != 0) {
            $where["type_id"] = $type_id;
        }
        if ($name != null) {
            $list = InfoModel::with("type")
                ->where($where)
                ->whereIn(InfoModel::getTable() . ".id", $asd)
                ->where(InfoModel::getTable() . ".name", "like", "%$name%")
                ->where("status", "normal")
                ->order("create_time", "desc")
                ->paginate();
        } else {
            $list = InfoModel::with("type")
                ->where($where)
                ->whereIn(InfoModel::getTable() . ".id", $asd)
                ->where("status", "normal")
                ->order("create_time", "desc")
                ->paginate();
        }

        // 返回成功的响应，并包含分页数据
        $this->success('获取成功', $list);
    }


    /**
     * 获取新闻关注列表
     */
    public function likesList($type_id = 0, $name = null)
    {
        // 获取当前用户收藏的数据
        $collect = ZanModel::where("user_id", $this->auth->id)->select();

        // 提取出用户收藏的 in_id 列表
        $asd = [];
        foreach ($collect as $key => $value) {
            $asd[] = $value->in_id;
        }
        $where = [];
        if ($type_id != 0) {
            $where["type_id"] = $type_id;
        }
        if ($name != null) {
            $list = InfoModel::with("type")
                ->where($where)
                ->whereIn(InfoModel::getTable() . ".id", $asd)
                ->where(InfoModel::getTable() . ".name", "like", "%$name%")
                ->where("status", "normal")
                ->order("create_time", "desc")
                ->paginate();
        } else {
            $list = InfoModel::with("type")
                ->where($where)
                ->whereIn(InfoModel::getTable() . ".id", $asd)
                ->where("status", "normal")
                ->order("create_time", "desc")
                ->paginate();
        }

        // 返回成功的响应，并包含分页数据
        $this->success('获取成功', $list);
    }
}
